import { _decorator, BlockInputEvents, Camera, Component, director, EventMouse, EventTouch, find, Input, input, instantiate, Node, Prefab, UITransform, Vec3 } from 'cc';
import { Plant } from '../Plant';
import { PlantType } from '../Global';
import { Cell } from './Cell';
import { AudioManager } from './AudioManager';
import { Config } from './Config';
const { ccclass, property } = _decorator;

@ccclass('MouseManager')
export class MouseManager extends Component {
    private static _instance: MouseManager = null;
    @property([Prefab])
    public plantPrefabArray: Prefab[] = [];

    private currentPlant: Node;
    /**
     * 获取实例
     */
    public static get Instance(): MouseManager {
        return this._instance;
    }
    protected onLoad(): void {
        //确保全局只有一个实例
        if (MouseManager._instance == null) {
            MouseManager._instance = this;
        } else {
            console.log("MouseManager is already exist");
            this.node.destroy();
        }
        input.on(Input.EventType.MOUSE_MOVE, this.onMouseMove, this);
    }
    protected onDestroy(): void {
        if (MouseManager.Instance == this) {
            MouseManager._instance = null;
            this.node.destroy();
        }
        input.off(Input.EventType.MOUSE_MOVE, this.onMouseMove, this);
    }
    onMouseMove(event: EventMouse) {
        this.followCursor(event);
    }

    addPlant(plantType: PlantType, event: EventMouse): boolean {
        // 如果当前已存在当前植物对象，则不能继续添加
        if (this.currentPlant != null) return false;
        // 获取植物预制体
        let plantPrefab = this.getPlantPrefab(plantType);
        // 如果植物预制体不存在
        if (plantPrefab == null) {
            // 返回false表示添加失败
            return false;
        }
        // 实例化植物预制体
        this.currentPlant = plantPrefab;
        // 将植物设置为Game的子对象
        this.currentPlant.parent = find("Canvas/Game/Plants");
        this.followCursor(event);
        // 返回true表示添加成功
        return true;
    }
    getPlantPrefab(plantType: PlantType): Node {
        // 遍历植物预制体数组
        for (let plantPrefab of this.plantPrefabArray) {
            // 实例化植物预制体
            let plantNode = instantiate(plantPrefab);
            // 判断实例化后的植物节点的植物类型是否与传入的植物类型相同
            if (plantNode.getComponent(Plant).plantType == plantType) {
                // 如果相同，则返回该植物节点
                return plantNode
            } else {
                // 如果不同，则销毁该植物节点
                plantNode.destroy();
            };
        }
        // 如果遍历完所有预制体后仍未找到匹配项，则返回null
        return null;
    }
    /**
     * 跟随光标的函数方法
     * @param event 
     */
    followCursor(event: EventMouse) {
        let mousePos = event.getUILocation();
        //将鼠标位置转换为世界坐标
        let worldPos = new Vec3(mousePos.x, mousePos.y, 0);
        // 如果当前有植物对象，则设置其世界位置为鼠标对应的世界坐标
        if (this.currentPlant != null) this.currentPlant.setWorldPosition(worldPos);
    }
    onCellClick(cell: Cell) {
        // 如果当前植物对象为null，则直接返回
        if (this.currentPlant == null) return;
        // 设置当前植物对象的位置为点击格子的位置
        let isSuccess = cell.addPlant(this.currentPlant);
        // 如果植物添加成功，那就释放当前植物对象
        if (isSuccess) {
            this.currentPlant = null;
            AudioManager.Instance.playClip(Config.plant, 2);
        }
    }
}



